<?php

require_once('BaseModel.php');

/**
 * This class stores Article information, most of them being accessible and changeable through the 'data' array
 */
class Article_model extends BaseModel {

	function Article_model() {
        $publicdata = array(
			'writer',
			'title',
			'content');
        parent::BaseModel('cr_articles', 'article_id', $publicdata, false);
	}

	/**
	 * Loads an article based on his id
	 * Returns true on success, false otherwise
	 */
	function load_article_from_id($id) {
		$this->db->where('article_id', $id);
		$this->db->join('cr_users', 'writer = user_id');
		return $this->load();
	}

	/**
	 * Creates a new article using his infos
     * Returns  true if the article was successfully created
	 */
	function new_article($infos) {
		$checkeddata = array ();
		$checkeddata['publication_date'] = date('Y-m-d H:i:s');
		$checkeddata['last_update'] = date('Y-m-d H:i:s');

    	$result = parent::new_data($infos, $checkeddata);
    	if (is_array($result))
    		return $result;

		return true;
	}

	/**
	 * Stores one user's information in the database
	 * Returns true if the infos got saved, false if an error occured
	 */
	function save_article($id, $data) {
		return $this->save_data($id, $data, null);
	}
	
	/**
	 * Checks the validity of the infos stored in the $this->data array
	 * Returns true if each field is valid, false otherwise
	 */
	public function fields_are_valid($data) {
		$errormet = false;
		$errors = array();
		foreach ($data as $key => $value) {
			if (isset($value) && strlen($value) == 0) {
				$errors[$key] = $key.' cannot be empty';
				$errormet = true;
			}
		}
		if (isset($data['title']) && strlen($data['title']) > 50) {
			$errors['title'] = 'Title is too long';
			$errormet = true;
		}
		if ($errormet)
			return $errors;
		return true;
	}
	
	/**
	 * Returns an array of $count articles starting with the $first
	 * If $count is null, returns all values
	 * This array is sorted on the $sorted column
	 * $match is an array containg a key and a value which is to match.
	 * ie : $match = array ($field => $namematch)
	 */
	function get_articles($first = null, $count = null, $match = null, $sorted = 'publication_date') {
		$this->db->join('cr_users', 'writer = user_id');
		return $this->get_data($first, $count, $sorted, $match);
	}

	/**
	 * Returns the number of article
	 * $match is an array containg a key and a value which is to match.
	 * ie : $match = array ($field => $namematch)
	 */
	function get_article_count($match = null) {
		return $this->get_data_count($match);
	}
	
	/**
	 * Deletes the article $id from the database.
	 */
	function delete_article($id){
		$this->db->where('article_id', $id);
		$this->db->delete('cr_articles');
	}
	
	/**
	 * Returns whether the user $user_id is responsible for this article
	 */
	function is_responsible($user_id, $art_id) {
		$this->db->select('writer');
		$this->db->where('article_id', $art_id);
		$query = $this->db->get('cr_articles');
		$array = $query->row_array();
		return $user_id == $array['writer'];
	}
}
?>
